package tools;

import Entity.Message;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressWarnings("unused")
public class check {
    public check(){

    }
    //绑定
    public static void join_message(Message MessageDTO, HttpServletRequest request, HttpServletResponse response) throws IOException {
        try{
            Connection con=null;
            Statement stmt=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            con= DriverManager.getConnection(url,"root","root");
            stmt=con.createStatement();
            ResultSet rs=null;
            //查询是否有对应学号的账号
            //System.out.println(MessageDTO.getUsername());
            String sql="select * from shop_user where dgutnumber="+MessageDTO.getUsername();
            rs=stmt.executeQuery(sql);
            int flag  = 0;
            //如果是非空的
            //System.out.println(rs.wasNull());
            while(rs.next()){
                if (!(rs.getString("username").isEmpty())) {
                    instantiation.setSeesion(rs, 1, request, response);
                    System.out.println("登陆成功");
                    flag = 1;
                }
            }
            System.out.println("1");
            if(flag == 0)
            {
                int flag2 = 0;
                ResultSet rs2=null;
                String num = MessageDTO.getUsername();
                String sql2="select * from dgut_user where username='"+num+"'";
                rs=stmt.executeQuery(sql2);
                System.out.println(rs2);
                while(rs.next()){
                    if ((rs.getString("username").equals(num))) {
                        instantiation.setSeesion(rs, 2, request, response);
                        System.out.println("登陆成功");
                        flag2 = 1;
                    }
                }
                if(flag2 == 0) {
                    String sql3 = "insert into dgut_user" + "(username,name,type)" + "values(" + "'" + MessageDTO.getUsername() + "'" + "," + "'" + MessageDTO.getName() + "'" + "," + "'" + MessageDTO.getType() + "'" + ")";
                    stmt.executeUpdate(sql3);
                }
                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = formatter.format(currentTime);
                String sql4="insert into shop_user"+"(use_id,username,password,phone,img,sex,role,address,dgutnumber)"+"values("+"'"+date+"',"+"'"+MessageDTO.getName()+"'"+","+"'123456'"+","+"'1008611'"+","+"'dog.jpg'"+","+"'男'"+","+"'0'"+","+"'中国'"+","+"'"+MessageDTO.getUsername() +"'"+")";
                stmt.executeUpdate(sql4);
                //将信息存入成功后，将其取出并实例化到session中
                String sql5="select * from shop_user where username='"+MessageDTO.getUsername()+"'";
                rs=stmt.executeQuery(sql5);
                while(rs.next()) {
                    if (!(rs.getString("username").isEmpty())) {
                        instantiation.setSeesion(rs, 1, request, response);
                    }
                }
            }
            System.out.println("2");
            rs.close();
            stmt.close();
            response.sendRedirect("http://localhost:8080/show.jsp");
        }catch(Exception e){
            e.printStackTrace();
        }
        return;
    }



}
